package cn.gson.fengboot.controller;

import cn.gson.fengboot.aspect.MyLog;
import cn.gson.fengboot.model.pojos.*;
import cn.gson.fengboot.model.service.DvieService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/vie")
public class DvieController {

    @Autowired
    DvieService service;

    @RequestMapping("/selectvie")
    public List<Dbusvie> selectvie(Integer busId,HttpServletRequest request){
        return service.selectvie(busId,request);
    }

    @RequestMapping("/addvie")
    @MyLog(modul = "售前模块",desc = "新增竞争机会")
    public void addvie(String viefrom){
        JSONObject obj = (JSONObject)JSONObject.parse(viefrom);
        Dbusvie busvie=obj.getObject("viefrom",Dbusvie.class);
        List<Dbvandvpro> viepros = obj.getObject("viepros",List.class);
        System.out.println(busvie);
        System.out.println(viepros);
        service.addvie(busvie,viepros);
    }

    @RequestMapping("/detvie")
    @MyLog(modul = "售前模块",desc = "删除竞争机会")
    public void detvie(Integer bvieId){
        service.detvie(bvieId);
    }


    @RequestMapping("/selectallvie")
    public Map<String,Object> selectallvie(String vieName,Integer currentpage, Integer pagesize){

        Page<Object> obj = PageHelper.startPage(currentpage, pagesize);

        service.selectallvie(vieName);
        Map<String,Object> map = new HashMap<>();

        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }


    @RequestMapping("/selectallviepro")
    public Map<String,Object> selectallviepro(Integer vieId,String vproName,Integer currentpage, Integer pagesize){


        Page<Object> obj = PageHelper.startPage(currentpage, pagesize);

        service.selectallviepro(vieId,vproName);
        Map<String,Object> map = new HashMap<>();

        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    @RequestMapping("/addmyvies")
    @MyLog(modul = "售前模块",desc = "新增竞争公司")
    public void addmyvies(@RequestBody Dvie dvie){
        System.out.println(dvie.toString());
        service.addmyvies(dvie);
    }

    @RequestMapping("/selectmodel")
    public List<Dviepromodel> selectmodel(){
        return service.selectmodel();
    }

    @RequestMapping("/addviepro")
    @MyLog(modul = "售前模块",desc = "新增竞争产品")
    public void addviepro(@RequestBody Dviepro dviepro){
        System.out.println(dviepro.toString());
        service.addviepro(dviepro);
    }

    @RequestMapping("/trueaddvpromodel")
    @MyLog(modul = "售前模块",desc = "新增竞争产品规格")
    public void trueaddvpromodel(@RequestBody Dviepromodel dviepromodel){
        service.trueaddvpromodel(dviepromodel);
    }

    @RequestMapping("/selectById")
    public Dbusvie selectById(Integer bvieId){
        return service.selectById(bvieId);
    }

    @RequestMapping("/selectBybvieName")
    public Integer selectBybvieName(String bvieName, HttpServletRequest request){
        return service.selectBybvieName(bvieName,request);
    }
}
